﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;

namespace InhollandAirPortal {
  public class Logger {
    protected string ReqQueryString;
    protected DateTime LogDateTime;
    protected string RequestedUrl;
    protected string ServerName;
    protected string UserAgent;
    protected string UserIP;
    protected string UserAuthenticated;
    protected string UserName;
    protected HttpApplication HttpApplication;

    public Logger() {
    }

    public Logger(HttpApplication AHttpApplication) {
      HttpApplication = AHttpApplication;
      Load();
    }

    protected void Load() {
      /* Haal waarden uit HttpApplication op */
      ReqQueryString = (HttpApplication.Request.QueryString != null) ? HttpApplication.Request.QueryString.ToString() : String.Empty;
      LogDateTime = DateTime.Now;
      RequestedUrl = (HttpApplication.Request.Url != null) ? HttpApplication.Request.Url.ToString() : String.Empty;
      ServerName = String.Empty;
      if (HttpApplication.Request.ServerVariables["HTTP_REFERER"] != null) {
        ServerName = HttpApplication.Request.ServerVariables["HTTP_REFERER"].ToString();
      }
      UserAgent = (HttpApplication.Request.UserAgent != null) ? HttpApplication.Request.UserAgent : String.Empty;
      UserIP = (HttpApplication.Request.UserHostAddress != null) ? HttpApplication.Request.UserHostAddress : String.Empty;
      UserAuthenticated = (HttpApplication.User != null) ? HttpApplication.User.Identity.IsAuthenticated.ToString() : String.Empty;
      UserName = (HttpApplication.User!= null) ? HttpApplication.User.Identity.Name : String.Empty;
    }

    public void WriteLog(){
      WriteRequestLog();
    }

    private void WriteRequestLog() {
      /* Wegschrijven */
      int LogID = -1;
      using (LoyalityDB DB = new LoyalityDB()) {
        LogID = DB.Logging_WriteRequestLog(ReqQueryString, LogDateTime, RequestedUrl, ServerName, UserAgent, UserIP, UserAuthenticated, UserName);
      }
    }
  }

  public class ExceptionLogger : Logger {
    private Exception CurrentException;

    public ExceptionLogger(HttpApplication AHttpApplication, Exception AException) {
      CurrentException = AException;
      HttpApplication = AHttpApplication;
      Load();      
    }

    new public void WriteLog() {
      WriteExceptionLog();
    }
    
    private void WriteExceptionLog() {
      ///<summary> 
      ///Schrijft waarden van meegegeven HttpApplication en Exception naar de database
      ///</summary>
      /* Exeption params */
      string Message = CurrentException.Message;
      string Source = CurrentException.Source;
      string TargetSite = CurrentException.TargetSite.ToString();
      string StackTrace = CurrentException.StackTrace;

      /* Wegschrijven */
      int ErrorID = -1;
      using (LoyalityDB DB = new LoyalityDB()) {
        ErrorID = DB.Logging_LogApplicationError(ReqQueryString, LogDateTime, RequestedUrl, ServerName, UserAgent, UserIP, UserAuthenticated, UserName, Message, Source, TargetSite, StackTrace);
      }
      if (ErrorID <= 0) {
        // Er is iets fout gegaan bij het wegschrijven naar database. (optioneel) Schrijf error log weg naar alternatieve locatie
      }
    }
  }


}